package com.zhongkai.listener;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.zhongkai.Bean.ContrastBean;
import com.zhongkai.Bean.UnitStartBean;
import com.zhongkai.app.service.DeviceOperationService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.io.IOException;

public class MyMessageListener implements MessageListener {


    @Autowired
    private DeviceOperationService deviceOperationService;


    @Override
    public void onMessage(Message message) {

        try {
            TextMessage textMessage = (TextMessage) message;
            //取消息内容
            String json = textMessage.getText();
            // System.out.println("1:::"+json);
            //System.out.println(json);
            if (json.contains("UnitStartBean:")) {  //与设备正式建立连接的消息
                json = json.replace("UnitStartBean:", "");
                //  json = json.replace(";",",");
                com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();

                UnitStartBean unitStartBean = mapper.readValue(json, UnitStartBean.class);

                /**
                 * 设备上线接口
                 *
                 * @param deviceID    设备uuid
                 * @param device_IP   设备ip
                 * @param set_status  设备状态 1 开机 2 关机 3离线
                 * @param is_warning  是否报警  1 正常 2 报警
                 * @param battery     总耗电量
                 * @param device_ver  版本号
                 * @param device_code 版本号标识
                 * */


                deviceOperationService.deviceAdd(
                        unitStartBean.getUnit_uuid(),
                        unitStartBean.getUnit_ip(),
                        unitStartBean.getSet_status(),
                        unitStartBean.getIs_warning(),
                        unitStartBean.getAll_electricity(),
                        unitStartBean.getVersions(),
                        unitStartBean.getSim()
                );
            } else if (json.contains("handlerRemoved:")) {  //设备下线
                json = json.replace("handlerRemoved:", "");

                deviceOperationService.deviceDown(json);
            } else if (json.contains("Update:")) { //设备升级结果
                json = json.replace("Update:", "");

                String[] parent = json.split(";");

                try {
                    String contrast = ContrastBean.contrast(parent[0]); //0位状态码  1位设备uuid

                    deviceOperationService.deviceUpdate(parent[0], parent[1], contrast);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        } catch (JMSException e) {
            e.printStackTrace();
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //----------------------------

/*
    else if (json.contains("BigUnitBean:")) {
        json = json.replace("BigUnitBean:", "");

        com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();

        BigUnitBean bigUnitBean = mapper.readValue(json, BigUnitBean.class);

        String unit_json = mapper.writeValueAsString(bigUnitBean); //实时数据

        String value = (String) redisUtils.get(StringStatic.REDIS_UNIT + bigUnitBean.getUnit_uuid()); //缓存数据



        if (StringUtils.isNullOrEmpty(value)){

            long millis = new DateTime().getMillis(); //获取当前系统时间戳

            bigUnitBean.setInsertionTime(millis);//存入时间



            redisUtils.set(StringStatic.REDIS_UNIT + bigUnitBean.getUnit_uuid(),unit_json,300); //缓存300秒数据

            // 数据存入数据库

            // 1设置存入时间

        }else {
            //数据不为空
            // 1取出数据  对比存入

            BigUnitBean unitBean = mapper.readValue(value, BigUnitBean.class);

            System.out.println(bigUnitBean.getTime());
            System.out.println(unitBean.getInsertionTime());
            if ( bigUnitBean.getTime() > unitBean.getInsertionTime()){

                long time = bigUnitBean.getTime() - unitBean.getInsertionTime();
                System.out.println(time);

                if (time > 1800000){ //大于30分钟 存入数据库



                }else {  //小于30分钟 存入缓存
                    redisUtils.set(StringStatic.REDIS_UNIT + bigUnitBean.getUnit_uuid(),unit_json,300); //缓存300秒数据
                }
            }
            //  long l =  - ;//时间差值计算 超过30分钟 存入如数据库更新数据  否则只存入当前数据

*//*                    String unit_json = mapper.writeValueAsString(bigUnitBean);
                    redisUtils.set(StringStatic.REDIS_UNIT + bigUnitBean.getUnit_uuid(),unit_json,300); //缓存30秒数据*//*
        }



    }*/

}
